﻿package br.com.ir.tcc.geco.modelo;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.sourceforge.yamlbeans.YamlException;
import net.sourceforge.yamlbeans.YamlReader;

import org.apache.commons.lang.StringUtils;

/**
 * <p>Combinacoes de componentes do modelo e templates, formando uma apresentacao.</p>
 */
@Entity
@Table(name = "modelo", catalog = "tcc")
public class Modelo implements java.io.Serializable {

	private static final long serialVersionUID = 5979475646464852381L;
	
	/**
	 * Nome do atributo que guarda identificador usado na associa��o de template 
	 */
	public static final String IDENTIFICACAO_OBJETO_YAML = "yamlObjectKey";
	
	/**
	 * Caracter usado para separar n�veis de hierarquia
	 */
	public static final String SEPARADOR_HIERARQUIA = ".";
	
	private Integer id;
	private String descricao;
	private String yaml;
	
	private Object parsedModel;
	private boolean updateParsedModel;

	private Set<Combinacao> combinancoes = new HashSet<Combinacao>(0);

	public Modelo() {
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "descricao", length = 50)
	public String getDescricao() {
		return this.descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "modelo")
	public Set<Combinacao> getCombinancoes() {
		return this.combinancoes;
	}

	public void setCombinancoes(Set<Combinacao> combinacaos) {
		this.combinancoes = combinacaos;
	}
	
	public String getYaml() {
		return yaml;
	}

	public void setYaml(String yaml) {
		this.yaml = yaml;
		updateParsedModel = true;
	}
	
	/**
	 * Retorna modelo escrito em YAML em forma de Objeto
	 * @return
	 * @throws YamlException
	 */
	@Transient
	public Object getObjectModel() throws YamlException {
		if(parsedModel == null || updateParsedModel){
			parsedModel = new YamlReader(yaml).read();
			Map map = (Map) parsedModel;
			Set<Entry<String, Object>> entrySet = map.entrySet();
			for (Entry<String, Object> entry : entrySet) {
				identifyObjects(entry.getValue(),entry.getKey());
			}
		}
		return  parsedModel;
	}
	
	private void identifyObjects(Object value, String currentPath){
		
		if(value instanceof Map){
			
			Map map = (Map) value;
			map.put(IDENTIFICACAO_OBJETO_YAML, currentPath);
			
			Set<Entry<String, Object>> entrySet = map.entrySet();
			
			for (Entry<String, Object> entry : entrySet) {
				
				identifyObjects(entry.getValue(), 
						currentPath + 
						(StringUtils.isNotEmpty(currentPath) ? SEPARADOR_HIERARQUIA : "") + 
						entry.getKey());
				
			}
			
		}		
		
	}

}
